<template>
  <div>
    <div class="search-wraper">
      <input v-model="keyword" type="text" placeholder="请输入城市或拼音">
    </div>
    <div class="search-container" ref="search" v-show="keyword">
      <div>
        <div class="seach-item"  v-for="item of list" :key="item.id">{{item.name}}</div>
        <div v-show="hasNoData">暂无数据</div>
      </div>
    </div>
  </div>
</template>

<script type="es6">
import BScroll from 'better-scroll'
export default {
  name: 'search',
  props: {
    cities: Object
  },
  data () {
    return {
      keyword: '',
      list: [],
      timer: null
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.search)
  },
  computed: {
    hasNoData () {
      return !this.list.length
    }
  },
  // 侦听器
  watch: {
    keyword () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (!this.keyword) {
        this.list = []
        return
      }
      this.timer = setTimeout(() => {
        const result = []
        for (let i in this.cities) {
          this.cities[i].forEach((value) => {
            if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  }
}
</script>

<style scoped lang="less">
.search-wraper{
  background: #00bcd4;
  text-align:center;
  padding-bottom:.12rem;
  input{
   width: 92%;
    text-align: center;
    padding: .10rem .12rem;
    border: none;
    border-radius: 1rem;
  }
}
.search-container{
  width: 100%;
  overflow: hidden;
  background-color: #eee;
  position: absolute;
  top: 1.52rem;
  left: 0;
  bottom: 0;
  z-index: 1;
  .seach-item{
    background-color: #fff;
    padding: .08rem .12rem;
  }
}
</style>
